<template>
  <el-cascader :props="propsList" clearable :show-all-levels="false" @change="onChange" v-model="selectValue" />
</template>

<script lang="ts">
import { defineComponent } from "vue";
export default defineComponent({
  name: "DataView",
})
</script>
<script lang="ts" setup>
import { request } from "@/utils/axios";
import { ref, withDefaults, computed } from "vue";
import { arrayToTree, mapTree } from "@/config/globalMethod"
import { OrgDataClass } from "./config"
import { appState } from "@/store/app";
let emit = defineEmits<{
  (e: "update:selectValue", value: any): void
}>()
let props = withDefaults(defineProps<{
  /**
   *选择的数据
   */
  selectValue: string | number;
}>(), {
  selectValue: ""
})
let selectValue = ref(props.selectValue)
/**
 * 获取列表
 */
/**
 * 动态加载
 */
const propsList = {
  lazy: true,
  value: "Id",
  label: "Name",
  lazyLoad(node: any, resolve: any) {
    const { level } = node
    let nodes
    if (level == 0) {
      // request("/Query/Get", {
      //   DocType: "RegionOrg",
      //   ActionType: "MeRegionOrg",
      //   ApiType: "SingleList"
      // })
      //   .then((res: any) => {
      //     let resList = arrayToTree(res)
      //     nodes = resList.map((item: any) => {
      //       return new OrgDataClass(item);
      //     });
      //     resolve(nodes)
      //   })
      //   .finally(() => {
      //   });
      if(appState.orgListAll.length>0){
             let resList:any = arrayToTree(appState.orgListAll)
          nodes = resList.map((item: any) => {
            return new OrgDataClass(item);
          });
          resolve(nodes)
      }
    }

    // if (level == 0) {
    //   nodes = productList.value.map((item: any) => {
    //     return {
    //       value: item.Id,
    //       label: item.Name,
    //       leaf: false
    //     }
    //   })
    //   resolve(nodes)
    // } else {
    //   queryGet({
    //     docType: "Product",
    //     apiType: "SingleList"
    //   }, {
    //     ActionType: "ProductSpec",
    //     Id: node.value
    //   }).then((res) => {
    //     nodes = res.map((res: any) => {
    //       return {
    //         value: res.Id,
    //         label: res.Name,
    //         leaf: true
    //       }
    //     })
    //     console.log("newnodes", nodes);
    //     resolve(nodes)
    //   }).catch()
    // }

    // setTimeout(() => {
    //   const nodes = Array.from({ length: level + 1 }).map((item: any) => ({
    //     value: ++id,
    //     label: `Option - ${id}`,
    //     leaf: level >= 2,
    //   }))
    //   console.log("nodesNew", nodes);
    //   resolve(nodes)
    // }, 1000)
  },
}
/**
 * 当选项发生变化时
 */
function onChange(value: any) {
  //取数组最后一项
  let selectValue = value.pop()
  emit("update:selectValue", selectValue);
}
</script>